package com.example.controller;

import com.example.common.Jr;
import com.example.dto.UserDto;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author  guochao
 * @date  2022/1/6 20:52
 * @version 1.0
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/user/list")
    public String list() {
        return "user/user-list";
    }

    @RequestMapping("/user/ajax/list")
    @ResponseBody
    public Jr ajaxList(@RequestBody UserDto userDto) {
        userService.listPage(userDto);
        return Jr.yesd(userDto);
    }

    @RequestMapping("/user/save")
    public String save(Long id, Model model) {
        if (id != null) {
            UserDto data = userService.getById(id);
            model.addAttribute("data", data);
        }
        return "user/user-save";
    }

    @RequestMapping("/user/ajax/save")
    @ResponseBody
    public Jr ajaxSave(User user) {
        userService.save(user);
        return Jr.yes();
    }

    @RequestMapping("/user/ajax/delete")
    @ResponseBody
    public Jr ajaxDelete(@RequestParam String id) {
        userService.delete(id);
        return Jr.yes();
    }
}
